package util;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

import javax.sql.DataSource;

import com.alibaba.druid.pool.DruidDataSource;

public class DBPoolUtils {
	//1.创建私有的静态的数据源
	private static DataSource ds;
	//2.静态块，初始化c
	static {
		try {
			Properties p = new Properties();
			p.load(new FileInputStream("./src/jdbc.properties"));
			String driver = p.getProperty("jdbc.driver");
			String url = p.getProperty("jdbc.url");
			String username = p.getProperty("jdbc.username");
			String password = p.getProperty("jdbc.password");
			
			// 使用连接池
			DruidDataSource dataSource = new DruidDataSource();
			dataSource.setDriverClassName(driver);
			dataSource.setUrl(url);
			dataSource.setUsername(username);
			dataSource.setPassword(password);
			
			dataSource.setMinIdle(10);
			dataSource.setInitialSize(10);
			dataSource.setMaxActive(30);
			dataSource.setLoginTimeout(60);
			
			// 数据源赋值
			ds = dataSource;
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}  catch (IOException e) {
			e.printStackTrace();
		} 
	}
	
	public static Connection getConnection() throws SQLException {
		return ds.getConnection();
	}
}
